package marke.barak.windows.composit;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import marek.barak.classes.Action;
import marek.barak.classes.ActionHddDAO;
import marek.barak.classes.ActionMade;
import marek.barak.classes.Data;
import marek.barak.classes.DataDAOHdd;
import marek.barak.classes.SendMail;
import marek.barak.classes.User;
import marek.barak.interfaces.ActionsDAO;
import marek.barak.interfaces.DataDAO;
import marek.barak.windows.main.main;
import marek.barak.exceptions.InvalidMeasurementDataException;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.TableColumn;


public class MeasurementDataLoad extends Composite {
	private Text text;
	private Text text_1;
	private ActionsDAO actions;
	private DataDAO datas;
	

	
	//private List<TableItem> actionsDone;
	private Table table;
	
	private void createText(int x,int y){
		if(text!=null){
			if(!text.isDisposed())
			text.dispose();
		}
		text = new Text(this,SWT.BORDER);
		text.setBounds(x,y,150,20);
		
	}
	private void removeText(){
	if(text!=null){
		if(!text.isDisposed())
			text.dispose();
	}
	}

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public MeasurementDataLoad(Composite parent, int style) {
		super(parent, style);
		datas = new DataDAOHdd();
		//combos = new ArrayList<Combo>();
		//text = new Text(getShell(),SWT.BORDER);
		//actionsDone = new ArrayList<TableItem>();
		try{
		actions = new ActionHddDAO();
		}catch(Exception e){
			MessageBox msg = new MessageBox(getShell(),SWT.ICON_ERROR|SWT.ABORT);
			msg.setMessage(e.getMessage());
			msg.setText("Error");
		}
		setLayout(new GridLayout(3, false));
			
		
		Label lblRecievedOn = new Label(this, SWT.NONE);
		lblRecievedOn.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.BOLD));
		lblRecievedOn.setText("Recieved ON");
		
		final DateTime dateTimeRecieved = new DateTime(this, SWT.BORDER);
		
		Label lblDataCheckPerformed = new Label(this, SWT.NONE);
		lblDataCheckPerformed.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		lblDataCheckPerformed.setText("Data check performed by");
		lblDataCheckPerformed.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.BOLD));
		
		Label lblAnnoucementD = new Label(this, SWT.NONE);
		lblAnnoucementD.setText("Annoucement date");
		lblAnnoucementD.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.BOLD));
		
		final DateTime dateTimeAnnoucementD = new DateTime(this, SWT.BORDER);
		
		final Combo combo = new Combo(this, SWT.NONE);
		for(User u:main.users.getAllUsers()){
			combo.add(u.toString());
		}
		combo.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		
		Label lblTargetDate = new Label(this, SWT.NONE);
		lblTargetDate.setText("Target date");
		lblTargetDate.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.BOLD));
		
		final DateTime dateTimeTargetDate = new DateTime(this, SWT.BORDER);
		
		Label lblDateOfCheck = new Label(this, SWT.NONE);
		lblDateOfCheck.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		lblDateOfCheck.setText("Date of Check");
		lblDateOfCheck.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.BOLD));
		
		final Label lblActualReleasedate = new Label(this, SWT.NONE);
		lblActualReleasedate.setText("Actual Release date");
		lblActualReleasedate.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.BOLD));
		
		final DateTime dateTimeActualReleaseD = new DateTime(this, SWT.BORDER);
		
		final DateTime dateTime = new DateTime(this, SWT.BORDER);
		dateTime.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		
		final Label lblDeliveredOnTime = new Label(this, SWT.NONE);
		lblDeliveredOnTime.setText("Delivered on time!");
		lblDeliveredOnTime.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.BOLD));
		new Label(this, SWT.NONE);
		new Label(this, SWT.NONE);
		
		Label labelComents = new Label(this, SWT.NONE);
		labelComents.setText("Comments");
		labelComents.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.BOLD));
		
		text_1 = new Text(this, SWT.BORDER);
		GridData gd_text_1 = new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1);
		gd_text_1.widthHint = 183;
		text_1.setLayoutData(gd_text_1);
		new Label(this, SWT.NONE);
		
		Label lblAction = new Label(this, SWT.NONE);
		lblAction.setToolTipText("Action Done");
		lblAction.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		lblAction.setText("Action");
		lblAction.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.BOLD));
		
		Label lblAmounth = new Label(this, SWT.NONE);
		lblAmounth.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		lblAmounth.setToolTipText("");
		lblAmounth.setText("Amounth");
		lblAmounth.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.BOLD));
				new Label(this, SWT.NONE);
				
				final Combo combo_1 = new Combo(this, SWT.NONE);
				combo_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
				for(Action a:actions.getActions())
					combo_1.add(a.getName());
		
				final Spinner spinner = new Spinner(this, SWT.BORDER);
				spinner.setMaximum(1000);
				GridData gd_spinner = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1);
				gd_spinner.widthHint = 105;
				spinner.setLayoutData(gd_spinner);
				
				gd_spinner.widthHint = 105;
				spinner.setLayoutData(gd_spinner);
		
		Button btnAdd = new Button(this, SWT.NONE);
		GridData gd_btnAdd = new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1);
		gd_btnAdd.widthHint = 133;
		btnAdd.setLayoutData(gd_btnAdd);
		
		btnAdd.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try{
					testAction(combo_1.getSelectionIndex(), spinner.getSelection());
					TableItem i = new TableItem(table,SWT.NONE);
					i.setText(new String[]{combo_1.getText(),Integer.toString(spinner.getSelection())});
				}catch(InvalidMeasurementDataException ex){
					
				MessageBox msg = new MessageBox(getShell(),SWT.ICON_ERROR|SWT.ABORT);
				msg.setMessage(ex.getMessage());
				msg.setText("Error");
				msg.open();
				}
					
			}
		});
		btnAdd.setToolTipText("Add new Action");
		btnAdd.setText("ADD");
		
		table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
	
		GridData gd_table = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1);
		gd_table.widthHint = 725;
		table.setLayoutData(gd_table);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		final TableColumn tblclmnActionDone = new TableColumn(table, SWT.NONE);
		tblclmnActionDone.setWidth(175);
		tblclmnActionDone.setText("Action Done");
		
		final TableColumn tblclmnAmounth = new TableColumn(table, SWT.NONE);
		tblclmnAmounth.setWidth(167);
		tblclmnAmounth.setText("Amounth");
		
		Button btnDelete = new Button(this, SWT.NONE);
		btnDelete.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				table.remove(table.getSelectionIndices());
			}
		});
		GridData gd_btnDelete = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1);
		gd_btnDelete.widthHint = 90;
		btnDelete.setLayoutData(gd_btnDelete);
		btnDelete.setText("Delete");
		new Label(this, SWT.NONE);
		
		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@SuppressWarnings("deprecation")
			@Override
			public void widgetSelected(SelectionEvent e) {
				try{
				checkErrors((text!=null &&!text.isDisposed()),text_1.getText() , combo.getSelectionIndex());
				List<ActionMade> zoznam = new ArrayList<ActionMade>();
				for(TableItem item : table.getItems()){
					zoznam.add(ActionMade.getInstance(actions.getAction(combo_1.indexOf(item.getText(0))),Integer.parseInt(item.getText(1))));
				}
				Data.Builder.setActions(zoznam);
				Data.Builder.setRecievedOn(new Date(dateTimeRecieved.getYear(),dateTimeRecieved.getMonth(),dateTimeRecieved.getDay()));
				Data.Builder.setAnnoucementDate(new Date(dateTimeAnnoucementD.getYear(),dateTimeAnnoucementD.getMonth(),dateTimeAnnoucementD.getDay()));
				Data.Builder.setActualReleaseDate(new Date(dateTimeActualReleaseD.getYear(),dateTimeActualReleaseD.getMonth(),dateTimeAnnoucementD.getDay()));
				Data.Builder.setTargetDate(new Date(dateTimeTargetDate.getYear(),dateTimeTargetDate.getMonth(),dateTimeTargetDate.getDay()));
				if(text!=null)
				Data.Builder.setDeliveredOnTime(text.getText());
				Data.Builder.setComents(text_1.getText());
				Data.Builder.setDataChceckPerformedBy(combo.getText());
				Data.Builder.setDateofBuddycheck(new Date(dateTime.getYear(),dateTime.getMonth(),dateTime.getDay()));
				Data day = Data.Builder.build();
				SendMail mail = new SendMail();
				mail.send(day.toString());
				datas.saveData(day);
				}catch(InvalidMeasurementDataException ex){
					MessageBox msg = new MessageBox(getShell(),SWT.ICON_ERROR|SWT.ABORT);
					msg.setText("Error");
					msg.setMessage(ex.getMessage());
					msg.open();
					
				}catch(Exception ex){
					ex.printStackTrace();
				}
			}
		});
		btnNewButton.setToolTipText("");
		GridData gd_btnNewButton = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1);
		gd_btnNewButton.widthHint = 90;
		btnNewButton.setLayoutData(gd_btnNewButton);
		btnNewButton.setText("Process");
		
	
		
		
	
		
		dateTimeActualReleaseD.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				boolean decision = true;
				
				if(dateTimeActualReleaseD.getYear()>dateTimeTargetDate.getYear()){
					decision = false;
				}else if(dateTimeActualReleaseD.getMonth()>dateTimeTargetDate.getMonth()){
					decision = false;
				}else if(dateTimeActualReleaseD.getDay()>dateTimeTargetDate.getDay()){
					decision = false;
			}
				
			if(decision){
				lblDeliveredOnTime.setText("Delivered on time!");
				removeText();
				}
				else{
				createText(dateTimeActualReleaseD.getLocation().x,lblDeliveredOnTime.getLocation().y);
				lblDeliveredOnTime.setText("Reason: ");

				}
	
		}});
	

	}
	private void testAction(int a,int v) throws InvalidMeasurementDataException{
		if(a==-1)
			throw new InvalidMeasurementDataException("No action has been choosen");
		if(v <=0)
			throw new InvalidMeasurementDataException("Value cant be 0 or negative");
		}
		
	private void checkErrors(boolean stav,String reason,int buddyChecker) throws InvalidMeasurementDataException
	{
		if(stav && reason.length()==0)
			throw new InvalidMeasurementDataException("Fill reason field if actual release day is afther target day");
		if(buddyChecker==-1)
			throw new InvalidMeasurementDataException("Choose an buddy checker");
			
		
		
	};
	

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}